const http = require("http");

const fs = require("fs");

const path = require("path");

var dataPath = path.join(__dirname, "/data.json");

http.createServer((req, res) => {
    var str = "";
    req.on("data", (chunk) => {
        str += chunk;
    });
    req.on("end", () => {
        var data = fs.readFileSync(dataPath);
        var jdata = JSON.parse(data.toString("utf-8"));
        var ldata = jdata.userList;
        var jstr = JSON.parse(str);
        for (var i = 0; i < ldata.length; i++) {
            if (ldata[i].username === jstr.username && ldata[i].password === jstr.password) {
                res.end("登录成功");
                break;
            } else {
                if (i == ldata.length - 1) {
                    res.end("用户名、密码不正确");
                }
                continue;
            }
        }
    });
}).listen(8081);
console.log("serv is listening on port 8081");
